package com.lxl.edu.info.manager.controller;


import com.lxl.edu.info.manager.daomain.Student;
import com.lxl.edu.info.manager.service.StudentSevice;


import java.util.Scanner;

public class StudentController {

    //开启学生管理系统，学生管理系统搭建
    StudentSevice studentSevice = new StudentSevice();

    public void start() {
        li:
        while (true) {
            Scanner sc = new Scanner(System.in);

            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");

            String choice = sc.next();
            switch (choice) {
                case "1":
                    // System.out.println("1.添加学生");
                    addStudent();
                    break;
                case "2":
                    System.out.println("2.删除学生");
                    break;
                case "3":
                    System.out.println(" 3.修改学生");
                    break;
                case "4":
                    //System.out.println(" 4.查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println(" 5.感谢您使用学生管理系统，谢谢 ");
                    break li;
                default:
                    System.out.println("您输入的有误，请从新输入：");
                    break;
            }
        }

    }

    public void findAllStudent() {
        // 1. 调用业务员中的获取方法, 得到学生的对象数组
        Student[] students = StudentSevice.findAllStudent();
        // 2. 判断数组的内存地址, 是否为null
        if (students == null) {

            System.out.println("查无信息，请重新录入");
            return;
        }
        // 3. 遍历数组, 获取学生信息并打印在控制台
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null) {
                System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getBirthday());
            }
        }
    }

    public boolean addStudent() {
        //键盘录入学生信息


        Scanner sc = new Scanner(System.in);
        String id;
        while (true) {

            System.out.println("请输入学生id：");
            id = sc.next();
            boolean flag = studentSevice.isExists(id);
            if (flag) {
                System.out.println("学号已被占用，请重新输入：");
            } else {
                break;
            }
        }

        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生id：");
        String birthday = sc.next();

        //将学生信息封装为学生类对象
        Student st = new Student();

        st.setId(id);
        st.setName(name);
        st.setAge(age);
        st.setBirthday(birthday);

        //将学生对象，传递给StudentService(业务)中的addStudent

        boolean result = studentSevice.addStudent(st);

        //根据返回的boolean类型结果，在控制台打印成功/失败
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
        return result;
    }
}
